package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.cms.bean.User;
import com.briup.cms.bean.extend.UserExtend;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.UserService;
import com.briup.cms.mapper.UserMapper;
import com.briup.cms.util.MD5Utils;
import com.briup.cms.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wo'q'q'c
 * @description 针对表【cms_user】的数据库操作Service实现
 * @createDate 2024-11-18 15:52:41
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User queryById(Long id) {
        //1.有效参数判断
        if (id == null){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        //2.查找用户
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new ServiceException(ResultCode.DATA_NONE);
        }
        return user;
    }

    @Override
    public User login(String username, String password) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
        lqw.eq(User::getUsername, username)
                .eq(User::getPassword, password);
        User user = userMapper.selectOne(lqw);
        if (user == null) throw new ServiceException(ResultCode.USER_LOGIN_ERROR);
        return user;
    }

    @Override
    public void save(User user) {
        user.setPassword(MD5Utils.MD5(user.getPassword()));
        String username = user.getUsername();
        String password = user.getPassword();
        if(username == null || password == null
                || (username = username.trim()).isEmpty()
                || (password = password.trim()).isEmpty() ){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }

//        username = username.trim();
//        password = password.trim();
//        if("".equals(username) || "".equals(password))
//            throw new ServiceException(ResultCode.PARAM_IS_BLANK);

        user.setUsername(username);
        user.setPassword(password);
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getUsername,username);
        User u = userMapper.selectOne(qw);
        if(u != null) {
            throw new ServiceException(ResultCode.USERNAME_HAS_EXISTED);
        }

        user.setRegisterTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public IPage<UserExtend> query(Integer pageNum, Integer pageSize,
                                   String username, String status, Integer roleId, Integer isVip) {
        if(pageNum == null || pageSize == null){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        IPage<UserExtend> page=new Page<>(pageNum,pageSize);
        userMapper.queryAllUserWithRole(page,username,status,roleId,isVip);
        return page;
    }
//    @Override
//    public void setVip(Long id) {
//        //1.id判断是否有效
//        User user = userDao.selectById(id);
//        if (user == null)
//            throw new ServiceException(ResultCode.DATA_NONE);
//        //2.判断是否已经设置为vip
//        if (user.getIsVip() == 1)
//            throw new ServiceException(ResultCode.PARAM_IS_INVALID);
//    }

    @Override
    public void update(User user) {
        //1.id判断（非空且有效存在）
        Long id = user.getId();
        User dbUser = userMapper.selectById(id);
        if (id == null || dbUser == null)
            throw new ServiceException(ResultCode.PARAM_IS_INVALID);

        //2.username空白字符去除【实际开发中交给前端处理,此处纯粹配合学习】

        String newName = user.getUsername();
        String oldName = dbUser.getUsername();
        if (newName != null && !newName.equals(oldName)) {
            String trimName = newName.trim();
            if ("".equals(trimName))
                throw new ServiceException(ResultCode.PARAM_IS_INVALID);

            //更新username(已经去除前后空白字符)
            user.setUsername(trimName);

            //3.用户名唯一判断: 查询username是否唯一(除当前user外)
            LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
            qw.eq(User::getUsername, trimName);
            if (userMapper.selectOne(qw) != null)
                throw new ServiceException(ResultCode.USERNAME_HAS_EXISTED);

        }
        //4.更新用户信息
        userMapper.updateById(user);
    }

    @Override
    public void deleteByBatch(List<Long> ids) {
        if(ids == null || ids.isEmpty()){
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }
        userMapper.deleteBatchIds(ids);
    }
}




